﻿Option Explicit On
Option Strict On

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Imports ACOMDirector
Imports ACOMDirector.Hardware
Imports ACOMDirector.Hardware.ACOM2000
Imports ACOMDirector.Framework.Hardware
Imports ACOMDirector.Framework.Hardware.Remoting
Imports ACOMDirector.Configuration

Public Class ControlPanelLarge

#Region " Private Members "

    Private m_Controller As IController
    Private m_ControllerCallbacks As ControllerCallbacks
    Private m_AmplifierState As AmplifierState
    Private m_FeatureAvailability As FeatureAvailabilityState

    'Auto Determine Operate Mode
    Private m_DisableEvents As Boolean = False
    Private m_DoNotRefresh As Boolean = False

    'Matrix Display Line Indexes
    Private m_DisplayLine1Index As Integer
    Private m_DisplayLine2Index As Integer
    Private m_DisplayLine3Index As Integer
    Private m_DisplayLine4Index As Integer

    Private m_CurrentLocation As Drawing.Point

#End Region

#Region " Form Events "

    Private Sub MainForm_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        If m_ControllerCallbacks IsNot Nothing Then
            RemoveHandler m_ControllerCallbacks.StateChanged, AddressOf StateChangedHandler
            RemoveHandler m_ControllerCallbacks.ForwardPowerLedChanged, AddressOf ForwardPowerLedChangedHandler
            RemoveHandler m_ControllerCallbacks.ReflectedPowerLedChanged, AddressOf ReflectedPowerLedChangedHandler

            m_ControllerCallbacks = Nothing
        End If

        'Save the Window Location
        SystemConfiguration.Instance.ACOMConfiguration.LargeControlPanelLocationX = m_CurrentLocation.X
        SystemConfiguration.Instance.ACOMConfiguration.LargeControlPanelLocationY = m_CurrentLocation.Y
        SystemConfiguration.Instance.ACOMConfiguration.Save()

    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Attach the State Manager object
        m_ControllerCallbacks = Session.GetSession.ControllerCallbacks
        m_Controller = Session.GetSession.Controller
        AddHandler m_ControllerCallbacks.StateChanged, AddressOf StateChangedHandler
        AddHandler m_ControllerCallbacks.ForwardPowerLedChanged, AddressOf ForwardPowerLedChangedHandler
        AddHandler m_ControllerCallbacks.ReflectedPowerLedChanged, AddressOf ReflectedPowerLedChangedHandler

        'Setup the Matrix Display
        MatrixControl.SetLedStyle(LedStyle.Square)
        MatrixControl.LoadFontCollectionFromResource("ACOMDirector.LedMatrixSymbolFile.xml")

        m_AmplifierState = m_Controller.GetState

        m_DisplayLine1Index = MatrixControl.AddTextItem(m_AmplifierState.DisplayLine1, New Point(0, 0), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine2Index = MatrixControl.AddTextItem(m_AmplifierState.DisplayLine2, New Point(0, 8), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine3Index = MatrixControl.AddTextItem(m_AmplifierState.DisplayLine3, New Point(0, 16), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine4Index = MatrixControl.AddTextItem(m_AmplifierState.DisplayLine4, New Point(0, 24), ItemDirection.Right, ItemSpeed.Idle)

        'Populate the Measurement Metrics
        Dim DTMetricsA As New DataTable
        DTMetricsA.Columns.Add("Name", GetType(String))
        DTMetricsA.Columns.Add("Value", GetType(Integer))
        DTMetricsA.Rows.Add("Bias", MeasurementMetricTypes.Bias)
        DTMetricsA.Rows.Add("DC Input", MeasurementMetricTypes.DCInput)
        DTMetricsA.Rows.Add("Drive", MeasurementMetricTypes.Drive)
        DTMetricsA.Rows.Add("Frequency", MeasurementMetricTypes.Frequency)
        DTMetricsA.Rows.Add("Forward Power", MeasurementMetricTypes.ForwardPower)
        DTMetricsA.Rows.Add("Filament Voltage", MeasurementMetricTypes.FilamentVoltage)
        DTMetricsA.Rows.Add("Power Gain", MeasurementMetricTypes.PowerGain)
        DTMetricsA.Rows.Add("HV", MeasurementMetricTypes.HV)
        DTMetricsA.Rows.Add("IF Front Tube", MeasurementMetricTypes.IFFrontTube)
        DTMetricsA.Rows.Add("IF Rear Tube", MeasurementMetricTypes.IFRearTube)
        DTMetricsA.Rows.Add("Grid Current", MeasurementMetricTypes.GridCurrent)
        DTMetricsA.Rows.Add("Plate Current", MeasurementMetricTypes.PlateCurrent)
        DTMetricsA.Rows.Add("Screen Current", MeasurementMetricTypes.ScreenCurrent)
        DTMetricsA.Rows.Add("Mains Voltage", MeasurementMetricTypes.MainsVoltage)
        DTMetricsA.Rows.Add("Plate RF Voltage", MeasurementMetricTypes.PlateRFVoltage)
        DTMetricsA.Rows.Add("Reflected Power", MeasurementMetricTypes.ReflectedPower)
        DTMetricsA.Rows.Add("Screen Voltage", MeasurementMetricTypes.ScreenVoltage)
        DTMetricsA.Rows.Add("Front Tube Temp", MeasurementMetricTypes.FrontTubeTemp)
        DTMetricsA.Rows.Add("Rear Tube Temp", MeasurementMetricTypes.RearTubeTemp)
        DTMetricsA.Rows.Add("VSWR", MeasurementMetricTypes.VSWR)

        Dim DTMetricsB As New DataTable
        DTMetricsB.Columns.Add("Name", GetType(String))
        DTMetricsB.Columns.Add("Value", GetType(Integer))
        DTMetricsB.Rows.Add("Bias", MeasurementMetricTypes.Bias)
        DTMetricsB.Rows.Add("DC Input", MeasurementMetricTypes.DCInput)
        DTMetricsB.Rows.Add("Drive", MeasurementMetricTypes.Drive)
        DTMetricsB.Rows.Add("Frequency", MeasurementMetricTypes.Frequency)
        DTMetricsB.Rows.Add("Forward Power", MeasurementMetricTypes.ForwardPower)
        DTMetricsB.Rows.Add("Filament Voltage", MeasurementMetricTypes.FilamentVoltage)
        DTMetricsB.Rows.Add("Power Gain", MeasurementMetricTypes.PowerGain)
        DTMetricsB.Rows.Add("HV", MeasurementMetricTypes.HV)
        DTMetricsB.Rows.Add("IF Front Tube", MeasurementMetricTypes.IFFrontTube)
        DTMetricsB.Rows.Add("IF Rear Tube", MeasurementMetricTypes.IFRearTube)
        DTMetricsB.Rows.Add("Grid Current", MeasurementMetricTypes.GridCurrent)
        DTMetricsB.Rows.Add("Plate Current", MeasurementMetricTypes.PlateCurrent)
        DTMetricsB.Rows.Add("Screen Current", MeasurementMetricTypes.ScreenCurrent)
        DTMetricsB.Rows.Add("Mains Voltage", MeasurementMetricTypes.MainsVoltage)
        DTMetricsB.Rows.Add("Plate RF Voltage", MeasurementMetricTypes.PlateRFVoltage)
        DTMetricsB.Rows.Add("Reflected Power", MeasurementMetricTypes.ReflectedPower)
        DTMetricsB.Rows.Add("Screen Voltage", MeasurementMetricTypes.ScreenVoltage)
        DTMetricsB.Rows.Add("Front Tube Temp", MeasurementMetricTypes.FrontTubeTemp)
        DTMetricsB.Rows.Add("Rear Tube Temp", MeasurementMetricTypes.RearTubeTemp)
        DTMetricsB.Rows.Add("VSWR", MeasurementMetricTypes.VSWR)

        m_DisableEvents = True
        cboMetricA.BeginUpdate()
        cboMetricA.DataSource = DTMetricsA
        cboMetricA.ValueMember = "Value"
        cboMetricA.DisplayMember = "Name"
        cboMetricA.EndUpdate()

        cboMetricB.BeginUpdate()
        cboMetricB.DataSource = DTMetricsB
        cboMetricB.ValueMember = "Value"
        cboMetricB.DisplayMember = "Name"
        cboMetricB.EndUpdate()

        cboMetricA.SelectedValue = m_AmplifierState.MetricA
        cboMetricB.SelectedValue = m_AmplifierState.MetricB

        m_DisableEvents = False
        RefreshStatus()

    End Sub

    Private Sub MainForm_Move(sender As Object, e As System.EventArgs) Handles Me.Move
        If Not Me.WindowState = FormWindowState.Minimized Then
            m_CurrentLocation = New Drawing.Point(Me.Location.X, Me.Location.Y)
        End If
    End Sub

#End Region

#Region " Private Methods "

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Delegate Sub RefreshStatusDelegate()
    Private Sub RefreshStatus()
        If m_DoNotRefresh Then Exit Sub

        If Me.InvokeRequired Then
            ' if operating on a thread, invoke a delegate
            ' on the UI thread.
            Dim omd As RefreshStatusDelegate = _
            New RefreshStatusDelegate(AddressOf RefreshStatus)
            Dim arx As IAsyncResult = Me.BeginInvoke( _
              omd, New Object() {})
            Me.EndInvoke(arx)
            Return
        End If

        m_DisableEvents = True

        m_AmplifierState = m_Controller.GetState
        m_FeatureAvailability = m_Controller.GetFeatureAvailabilityState

        If Not m_AmplifierState.CanConnect Then
            cmdConnect.Enabled = False
        ElseIf Not m_Controller.GetCOMPortOpen Then
            cmdConnect.Enabled = True
        End If

        If Not cboMetricA.DroppedDown Then
            cboMetricA.SelectedValue = m_AmplifierState.MetricA
        End If

        If Not cboMetricB.DroppedDown Then
            cboMetricB.SelectedValue = m_AmplifierState.MetricB
        End If

        cmdAntenna0.Enabled = m_FeatureAvailability.SelectAntenna0Enabled
        cmdAntenna1.Enabled = m_FeatureAvailability.SelectAntenna1Enabled
        cmdAntenna2.Enabled = m_FeatureAvailability.SelectAntenna2Enabled
        cmdAntenna3.Enabled = m_FeatureAvailability.SelectAntenna3Enabled
        cmdAntenna4.Enabled = m_FeatureAvailability.SelectAntenna4Enabled
        cmdAntenna5.Enabled = m_FeatureAvailability.SelectAntenna5Enabled
        cmdAntenna6.Enabled = m_FeatureAvailability.SelectAntenna6Enabled
        cmdAntenna7.Enabled = m_FeatureAvailability.SelectAntenna7Enabled
        cmdAntenna8.Enabled = m_FeatureAvailability.SelectAntenna8Enabled
        cmdAntenna9.Enabled = m_FeatureAvailability.SelectAntenna9Enabled
        cmdConnect.Enabled = m_FeatureAvailability.ConnectEnabled
        cmdDisconnect.Enabled = m_FeatureAvailability.DisconnectEnabled
        cboMetricA.Enabled = m_FeatureAvailability.MetricASelectionEnabled
        cboMetricB.Enabled = m_FeatureAvailability.MetricBSelectionEnabled
        cmdOnOff.Enabled = m_FeatureAvailability.OnOffEnabled
        cmdOperateStandby.Enabled = m_FeatureAvailability.OperateStandbyEnabled
        cmdStartMeasuring.Enabled = m_FeatureAvailability.StartMeasuringEnabled
        cmdStopMeasuring.Enabled = m_FeatureAvailability.StopMeasuringEnabled

        MatrixControl.SetItemText(m_DisplayLine1Index, m_AmplifierState.DisplayLine1)
        MatrixControl.SetItemText(m_DisplayLine2Index, m_AmplifierState.DisplayLine2)
        MatrixControl.SetItemText(m_DisplayLine3Index, m_AmplifierState.DisplayLine3)
        MatrixControl.SetItemText(m_DisplayLine4Index, m_AmplifierState.DisplayLine4)

        ledPower.State = m_AmplifierState.PowerLED
        ledOperate.State = m_AmplifierState.OperateLED
        ledStandby.State = m_AmplifierState.StandbyLED
        ledPlate.State = m_AmplifierState.PlateLED
        ledDrive.State = m_AmplifierState.DriveLED
        ledHeater.State = m_AmplifierState.HeaterLED
        ledProtection.State = m_AmplifierState.ProtectionLED

        m_DisableEvents = False

    End Sub

    Private Sub Connect()
        cmdConnect.Enabled = False
        cmdDisconnect.Enabled = True
        m_Controller.Connect()
    End Sub

#End Region

#Region " Event Handlers "

    Private Sub ForwardPowerLedChangedHandler(ByVal sender As Object, ByVal e As PowerLedChangedEventArgs)
        meterForwardPower.Level = e.Level
    End Sub

    Private Sub ReflectedPowerLedChangedHandler(ByVal sender As Object, ByVal e As PowerLedChangedEventArgs)
        meterReflectedPower.Level = e.Level
    End Sub

    Private Sub StateChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        RefreshStatus()
    End Sub

    Private Sub cmdOperateStandby_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOperateStandby.Click
        m_Controller.OperateStandby()
    End Sub

    Private Sub cmdAntenna0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna0.Click
        m_Controller.SelectAntenna(0)
    End Sub

    Private Sub cmdAntenna1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna1.Click
        m_Controller.SelectAntenna(1)
    End Sub

    Private Sub cmdAntenna2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna2.Click
        m_Controller.SelectAntenna(2)
    End Sub

    Private Sub cmdAntenna3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna3.Click
        m_Controller.SelectAntenna(3)
    End Sub

    Private Sub cmdAntenna4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna4.Click
        m_Controller.SelectAntenna(4)
    End Sub

    Private Sub cmdAntenna5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna5.Click
        m_Controller.SelectAntenna(5)
    End Sub

    Private Sub cmdAntenna6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna6.Click
        m_Controller.SelectAntenna(6)
    End Sub

    Private Sub cmdAntenna7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna7.Click
        m_Controller.SelectAntenna(7)
    End Sub

    Private Sub cmdAntenna8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna8.Click
        m_Controller.SelectAntenna(8)
    End Sub

    Private Sub cmdAntenna9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna9.Click
        m_Controller.SelectAntenna(9)
    End Sub

    Private Sub cmdOnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOnOff.Click
        'If the power is already on then turn it off?
        m_AmplifierState = m_Controller.GetState

        If m_AmplifierState.PowerState = PowerState.Connected Then
            If MsgBox("Are you sure you want to turn off the ACOM2000A?", MsgBoxStyle.YesNo, "Turn Off ACOM2000A") = MsgBoxResult.Yes Then
                m_Controller.OnOff()
            End If
        ElseIf m_AmplifierState.PowerState = PowerState.PoweredOff Then
            If m_Controller.GetSupportsRemotePowerOn Then
                If MsgBox("Would you like to remotely turn on the ACOM2000A?", MsgBoxStyle.YesNo, "Turn On ACOM2000A") = MsgBoxResult.Yes Then
                    m_Controller.OnOff()
                End If
            End If
        End If
    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        cmdConnect.Enabled = False
        m_Controller.Connect()
    End Sub


    Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
        m_Controller.Disconnect()
        cmdConnect.Enabled = True
        cmdDisconnect.Enabled = False
    End Sub

    Private Sub cmdStartMeasuring_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartMeasuring.Click

        Dim MetricA As MeasurementMetricTypes = DirectCast(cboMetricA.SelectedValue, MeasurementMetricTypes)
        Dim MetricB As MeasurementMetricTypes = DirectCast(cboMetricB.SelectedValue, MeasurementMetricTypes)

        m_Controller.StartMeasuring(MetricA, MetricB)

    End Sub

    Private Sub cmdStopMeasuring_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopMeasuring.Click
        m_Controller.StopMeasuring()
    End Sub

    Private Sub cboMetricB_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMetricB.SelectedValueChanged
        If m_DisableEvents Then Exit Sub
        If IsNumeric(cboMetricB.SelectedValue) Then
            Dim MetricB As MeasurementMetricTypes = DirectCast(cboMetricB.SelectedValue, MeasurementMetricTypes)
            m_Controller.SetMetricB(MetricB)
        End If
    End Sub

    Private Sub cboMetricA_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMetricA.SelectedValueChanged
        If m_DisableEvents Then Exit Sub
        If IsNumeric(cboMetricA.SelectedValue) Then
            Dim MetricA As MeasurementMetricTypes = DirectCast(cboMetricA.SelectedValue, MeasurementMetricTypes)
            m_Controller.SetMetricA(MetricA)
        End If
    End Sub

#End Region

End Class
